using UnityEngine;
using Voodoo.Pattern;

public class OpponentHead : MonoBehaviour
{
	[SerializeField]
	private Rigidbody[] _rigs;

	private Progress progress;

	private GameplayAudio gameplayAudio;

	private GameplayHaptic gameplayHaptic;

	private float rotationSpeed;

	public int amount;

	private MapCreatorEditor _mapGenerator;

	private void Start()
	{
		SetKinematic(active: true);
		progress = Object.FindObjectOfType<Progress>();
		Object.FindObjectOfType<TapControl>()._headPos = base.transform;
		gameplayAudio = GameObject.Find("GameplaySounds").GetComponent<GameplayAudio>();
		gameplayHaptic = GameObject.Find("Manager").GetComponent<GameplayHaptic>();
		Invoke("UpdateRotationSpeed", 0.1f);
	}

	public static void AddPhysics(Collider other)
	{
		if (!other.gameObject.TryGetComponent<Rigidbody>(out var component))
		{
			component = other.gameObject.AddComponent<Rigidbody>();
		}
		component.constraints = (RigidbodyConstraints)56;
		component.mass = 0.1f;
		component.isKinematic = false;
		if (other.CompareTag("Gem"))
		{
			other.tag = "GemCollected";
		}
		else
		{
			other.tag = "Player";
		}
		component.AddForce(new Vector3(Random.Range(-1f, 1f), Random.Range(-2f, 0.5f), 0f), ForceMode.Impulse);
	}

	private void OnTriggerStay(Collider other)
	{
		Booster component;
		if (other.CompareTag("Finish") || other.CompareTag("Gem"))
		{
			if (_mapGenerator == null)
			{
				_mapGenerator = Object.FindObjectOfType<MapCreatorEditor>();
			}
			int maxExclusive = (_mapGenerator.IsBonusLevel ? 10 : (31 - amount));
			if (Random.Range(1, maxExclusive) == 1)
			{
				AddPhysics(other);
				if (SingletonMB<BonusLevelManager>.Instance.IsBonusLevelActive || Object.FindObjectOfType<MapCreatorEditor>().IsBonusLevel)
				{
					NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) + 1f);
					SingletonMB<BonusFX>.Instance.MoveAndPlay(other.transform.position + new Vector3(0f, 0f, -0.5f));
				}
				progress.KillBlock();
				gameplayAudio.PlaySound(GameplaySoundType.DigSound);
				gameplayHaptic.PlayHaptic(HapticType.Dig);
			}
		}
		else if (other.tag == "Booster" && other.TryGetComponent<Booster>(out component))
		{
			component.GetBooster();
		}
	}

	public void SetKinematic(bool active)
	{
		for (int i = 0; i < _rigs.Length; i++)
		{
			_rigs[i].isKinematic = active;
		}
	}

	public void UpdateRotationSpeed()
	{
		rotationSpeed = 0.9f + (float)amount * 0.1f;
		if (rotationSpeed > 1.5f)
		{
			rotationSpeed = 1.5f;
		}
		for (int i = 0; i < base.transform.childCount; i++)
		{
			base.transform.GetChild(i).TryGetComponent<Animator>(out var component);
			if (component != null)
			{
				component.SetFloat("speed", rotationSpeed);
				break;
			}
		}
	}
}
